import { metaMaskMode } from "@/store/metamask"
import { Toast } from "vant"


// 需要加载的文件列表
const fileList: Array<any> = [
    {
        prod: 1,
        dev: 1,
        fileName: 'eth'
    },
    {
        prod: 56,
        dev: 97,
        fileName: 'bsc'
    }
]
var fileIndex: number = -1
// 对应链文件列表
var locales: any
function getAbis() {
    const chainId = metaMaskMode().getChainId()

    fileList.forEach((item, index) => {
        if (process.env.NODE_ENV == 'production') {
            if (item.prod == chainId) {
                fileIndex = index
            }
        } else {
            if (item.dev == chainId) {
                fileIndex = index
            }
        }

        if (index == fileList.length - 1) {
            if (fileIndex == -1) {
                Toast('合约ABI配置无效')
                return
            }
        }
    })

    let obj: { [key: string]: any } = {}
    if (fileList[fileIndex]) {
        locales = require.context('../contract/', true, /[A-Za-z0-9-_,\s]+\.ts$/i)
        locales.keys().forEach((key: string) => {
            if (key.indexOf(fileList[fileIndex].fileName) != -1) {
                const matched = key.match(/([A-Za-z0-9-_]+)\./i)
                if (matched && matched.length > 1) {
                    const locale: string = matched[1]
                    obj[locale] = locales(key)
                    obj[locale].name = locale
                }
            }

        })
    }


    return obj



}
export default getAbis

